package org.jugile.restapi.domain;

import org.jugile.restapi.common.*;
import org.jugile.util.Buffer;
import org.jugile.util.Node;

public class Operation extends OperationBase implements org.jugile.util.IDO {

	
	public String resolveTmpl() {
		if ("null".equals(getTmpl())) return null;
		return getPathItem().resolveTmpl(getTmpl());
	}
	
	public String resolveModel() {
		if ("null".equals(getModel())) return null;
		return getPathItem().resolveModel(getModel());
	}
	
	/**
	 * @return Modelname without path.
	 */
	public String resolveModelName() {
		if (!empty(getModel())) return getModel();
		return getPathItem().resolveModelName();
	}
	/**
	 * @return Modelname without path and suffix:  Boo_model.xml => Boo_model
	 */
	private String getModelNameNoSuffix() {
		String mn = resolveModelName();
		if (mn == null) fail("unresolvable modelname");
		if (mn.endsWith(".xml")) mn = mn.substring(0, mn.length() -4);
		return mn;
	}
	
	public String resolveModelDir() {
		return getPathItem().resolveModelDir();
	}
	public String resolveDataModelFile(String name) {
		return resolveModelDir() + "/testdata/" + getModelNameNoSuffix() + "-" + name+".js";
	}
	
	public String resolveMimetype() {
		if (!empty(getMimetype())) return getMimetype();
		if (getType() != null) return getType().resolveMimetype();
		return "text/html";
	}
	
	public String resolveControllerClassName() {
		return getPathItem().resolveControllerClassName();
	}
	
	public String resolveTmplDir() {
		return getPathItem().resolveTmplDir();
	}
	public String resolveBorderFile() {
		String b = getBorder();
		if (!empty(b)) return resolveTmplDir() + "/" +b;
		b = getPathItem().resolveBorderFile();
		if (!empty(b)) return b;
		return resolveTmplDir() + "/Border.xml";
	}
	
	
	
	public boolean isMatch(String method, String type) {
		//print("ismatch: "+ method + " "+type);
		if (!eq(method,getMethod())) return false;
		if (!empty(type)) {
			Type t = getType();
			if (t == null) return false;
			return type.equals(t.getName());
		}
		return true;
	}

	private Api getApi() {
		return getPathItem().getApi();
	}
	
	public void parse(Node n) {
		Type t = null;
		if (!empty(n.attr("type"))) t = getApi().getType(n.attr("type"));
		setType(t);
		setMethod(n.name());
		if (!empty(n.attr("tmpl"))) setTmpl(n.attr("tmpl"));
		if (!empty(n.attr("model"))) setModel(n.attr("model"));
		if (!empty(n.attr("mime-type"))) setMimetype(n.attr("mime-type"));
		if (!empty(n.attr("border"))) setBorder(n.attr("border"));
	}
	
	public Node toXml() {
		Node n = new Node(getMethod());
		if (getType() != null) n.attr("type", getType().getName());
		if (!empty(getTmpl())) n.attr("tmpl",getTmpl());
		if (!empty(getModel())) n.attr("model",getModel());
		if (!empty(getMimetype())) n.attr("mime-type",getMimetype());
		if (!empty(getBorder())) n.attr("border", getBorder());
		return n;
	}
	
	public void dump(Buffer buf) {
		buf.incrIndent();
		buf.ln(getType().getSuffix() + " " +getMethod());
		buf.decrIndent();
	}

	
}